<template>
  <div class="ai-analysis">
    <div class="analysis-container">
      <div class="loading" v-if="loading">
        <div class="spinner"></div>
        <p>正在获取AI分析...</p>
      </div>
      
      <div class="error" v-if="error">
        <div class="error-icon">!</div>
        <p>获取分析失败: {{ error }}</p>
        <button @click="fetchAnalysis" class="retry-btn">重试</button>
      </div>
      
      <textarea 
        v-if="!loading && !error"
        class="analysis-text"
        :value="analysisText"
        readonly
        rows="8"
      ></textarea>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import axios from "axios";

// 在这里设置您的API配置
const apiUrl = ref('http://localhost:8090/ai-analysis/getAiData'); // 替换为实际API地址

const analysisText = ref('');
const loading = ref(false);
const error = ref(null);
const showConfig = ref(true); // 开发模式下显示配置，生产环境可设为false

// 获取AI分析
const fetchAnalysis = async () => {
  try {
    loading.value = true;
    error.value = null;
    
    // 模拟API请求 - 实际使用时替换为真实API调用
    // 真实API调用示例：
    const response = await axios.get(`${apiUrl.value}`);
    analysisText.value = response.data;
  } catch (err) {
    error.value = err.message || '网络请求失败';
    console.error('获取AI分析失败:', err);
  } finally {
    loading.value = false;
  }
};

// 组件挂载时自动获取分析
onMounted(() => {
  fetchAnalysis();
});
</script>

<style scoped>
.ai-analysis {

}

h1 {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 20px;
  font-size: 24px;
}

.analysis-container {
  height: 487px;
  background-color: #f9f9f9;
  border-radius: 8px;
  padding: 15px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.05);
  min-height: 200px;
  display: flex;
  flex-direction: column;
}

/* 自定义滚动条样式 */
.analysis-text {
  scroll-behavior: smooth;
}

.analysis-text::-webkit-scrollbar {
  width: 4px;
}

.analysis-text::-webkit-scrollbar-track {
  background: #1e1e20;
}

.analysis-text::-webkit-scrollbar-thumb {
  background: linear-gradient(to bottom, #0a84ff, #00c6ff);
  border-radius: 4px;
}

.analysis-text::-webkit-scrollbar-thumb:hover {
  background: linear-gradient(to bottom, #0056b3, #0099cc);
}

.analysis-text {
  color: #000000;
  width: 100%;
  height: 100%;
  border: 1px solid #f8f6f6;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  font-size: 15px;
  line-height: 1.6;
  resize: vertical;
  background-color: #f9f9f9;
  box-sizing: border-box;
  white-space: pre-wrap;
}

.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 30px 0;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid rgba(0, 0, 0, 0.1);
  border-radius: 50%;
  border-top-color: #3498db;
  animation: spin 1s ease-in-out infinite;
  margin-bottom: 15px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.error {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  top: 80px;
  color: #e74c3c;
  text-align: center;
}

.error-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #e74c3c;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 15px;
}

.retry-btn, .fetch-btn {
  margin-top: 15px;
  padding: 8px 20px;
  background-color: #3498db;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s;
}

.retry-btn:hover, .fetch-btn:hover {
  background-color: #2980b9;
}

/* API配置区域 */
.api-config {
  margin-top: 30px;
  padding: 15px;
  background-color: #f0f8ff;
  border-radius: 8px;
  border: 1px solid #d0e8ff;
}

.api-config h3 {
  margin-top: 0;
  color: #2c3e50;
  border-bottom: 1px solid #d0e8ff;
  padding-bottom: 10px;
}

.config-item {
  margin-bottom: 15px;
}

.config-item label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
  color: #3498db;
}

.config-item input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #d0e8ff;
  border-radius: 4px;
  font-size: 14px;
}
</style>